package com.itheima.googleplay9.holder;

import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import com.itheima.googleplay9.R;
import com.itheima.googleplay9.bean.AppInfo;
import com.itheima.googleplay9.global.GooglePlayApp;
import com.itheima.googleplay9.manager.DownloadInfo;
import com.itheima.googleplay9.manager.DownloadManager;

import butterknife.Bind;

/**
 * Created by lxj on 2016/5/28.
 */
public class AppDownloadHolder extends BaseHolder<AppInfo> implements View.OnClickListener
        ,DownloadManager.DownloadObserver{
    @Bind(R.id.pb_download)
    ProgressBar pbDownload;
    @Bind(R.id.btn_download)
    Button btnDownload;

    @Override
    protected View initHolderView() {
        return View.inflate(GooglePlayApp.context, R.layout.layout_detail_app_download, null);
    }
    private AppInfo appInfo;
    @Override
    public void bindData(AppInfo data) {
        this.appInfo = data;

        //注册下载监听器
        DownloadManager.getInstance().registerDownloadObserver(this);
        btnDownload.setOnClickListener(this);

        //初始化当前的界面显示
        DownloadInfo downloadInfo = DownloadManager.getInstance().getDownloadInfo(appInfo);
        if(downloadInfo!=null){
            onDownloadStateChange(downloadInfo);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_download:
                DownloadInfo downloadInfo = DownloadManager.getInstance().getDownloadInfo(appInfo);
                if(downloadInfo==null){
                    //说明是从来没有下载过
                    DownloadManager.getInstance().download(appInfo);
                }else {
                    if(downloadInfo.getState()==DownloadManager.STATE_DOWNLOADING
                       || downloadInfo.getState()==DownloadManager.STATE_WAITING){
                        //应该暂停
                        DownloadManager.getInstance().pause(appInfo);
                    }else if(downloadInfo.getState()==DownloadManager.STATE_PAUSE
                            || downloadInfo.getState()==DownloadManager.STATE_ERROR){
                        //应该下载
                        DownloadManager.getInstance().download(appInfo);
                    }else if(downloadInfo.getState()==DownloadManager.STATE_FINISH){
                        //应该安装
                        DownloadManager.getInstance().installApk(appInfo);
                    }
                }
                break;
        }
    }

    @Override
    public void onDownloadProgressChange(DownloadInfo downloadInfo) {
        //判断当前的app和正在现在的是否是同一个，如果不是则返回
        if(appInfo==null || appInfo.getId()!=downloadInfo.getId()){
            return;
        }

        //1.计算下载百分比   形式：  1，2，,22,100
        float percent = downloadInfo.getCurrentLength()*100f/downloadInfo.getSize();
        //2.显示百分比
        btnDownload.setText((int)percent+"%");
        //去掉btnDownload的背景
        btnDownload.setBackgroundResource(0);
        //3.更新进度条
        pbDownload.setProgress((int) percent);
    }

    @Override
    public void onDownloadStateChange(DownloadInfo downloadInfo) {
        //判断当前的app和正在现在的是否是同一个，如果不是则返回
        if(appInfo==null || appInfo.getId()!=downloadInfo.getId()){
            return;
        }
        switch (downloadInfo.getState()){
                case DownloadManager.STATE_PAUSE:
                    btnDownload.setText("继续下载");
                    btnDownload.setBackgroundResource(0);
                    //1.计算下载百分比   形式：  1，2，,22,100
                    float percent = downloadInfo.getCurrentLength()*100f/downloadInfo.getSize();
                    //3.更新进度条
                    pbDownload.setProgress((int) percent);
                    break;
                case DownloadManager.STATE_FINISH:
                    btnDownload.setText("安装");
                    break;
                case DownloadManager.STATE_WAITING:
                    btnDownload.setText("等待中");
                    break;
                case DownloadManager.STATE_ERROR:
                    btnDownload.setText("出错，重下");
                    break;
        }
    }
}
